package edu.gznu.ics.fast.pulsar.service.prestoacc.taskService.ComputeTask.impl;

import edu.gznu.ics.fast.pulsar.dao.prestoacc.task.TaskIdMapper;
import edu.gznu.ics.fast.pulsar.dao.prestoacc.task.TaskParameterMapper;
import edu.gznu.ics.fast.pulsar.model.po.prestoacc.task.computeTask.TaskIdPO;
import edu.gznu.ics.fast.pulsar.model.po.prestoacc.task.computeTask.parameter.TaskParameterPO;
import edu.gznu.ics.fast.pulsar.model.vo.prestoacc.task.computeTask.parameter.EditTaskParaVO;
import edu.gznu.ics.fast.pulsar.model.vo.web.TaskIdQueryVO;
import edu.gznu.ics.fast.pulsar.model.vo.web.UserParaTree;
import edu.gznu.ics.fast.pulsar.service.prestoacc.taskService.ComputeTask.ComputeParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by fei-zyx on 2016/10/11.
 */
@Service("computeParameterServiceImply")
public class ComputeParameterServiceImply implements ComputeParameterService {
    @Autowired
    private TaskParameterMapper taskParameterMapper;
    @Autowired
    private TaskIdMapper taskIdMapper;

    public void showParaInfoByTaskId() {
        //taskParameterMapper.addComputeParameter();
        //taskParameterMapper.editComputeParameter();
        // taskParameterMapper.findComputeParameterByTaskId();

    }

    public int addNewTaskPara(EditTaskParaVO editTaskParaVO) {
    /* TaskParameterPO taskParameterPO = new TaskParameterPO();
    int maxId = taskIdMapper.findMaxComputeTaskId();
     TaskIdPO taskIdPO =new TaskIdPO();
     taskIdPO.setTaskId(maxId+1);
     taskIdPO.setTaskDate( new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
     taskIdPO.setUserId(1);
     taskIdPO.setValid(0);
     taskIdMapper.addnewComputeTaskId(taskIdPO);
     taskParameterPO.settaskId(maxId+1);
     taskParameterPO.setstep(1);
     taskParameterPO.setstepId(1);
     taskParameterPO.setparameter("rfifind  -time "+editTaskParaVO.getTime()+" -o test1 $fname");
     taskParameterMapper.addComputeParameter(taskParameterPO);

     taskParameterPO.settaskId(maxId+1);
     taskParameterPO.setstep(2);
     taskParameterPO.setstepId(1);
     taskParameterPO.setparameter("prepsubband  -numout $SPECTRA_PER_FILE/$DOWNSAMP -nsub $BYTES_PER_SPECTRA " +
             "-lodm " +editTaskParaVO.getLodm1()+
             " -dmstep " +editTaskParaVO.getDmstep1()+
             " -numdms " +editTaskParaVO.getNumdms1()+
             " -downsamp " +editTaskParaVO.getDmstep1()+
             " -mask test1_rfifind.mask -o test4 $fname");
     taskParameterMapper.addComputeParameter(taskParameterPO);

     taskParameterPO.settaskId(maxId+1);
     taskParameterPO.setstep(2);
     taskParameterPO.setstepId(2);
     taskParameterPO.setparameter("prepsubband  -numout $SPECTRA_PER_FILE/$DOWNSAMP -nsub $BYTES_PER_SPECTRA " +
             "-lodm " +editTaskParaVO.getLodm2()+
             " -dmstep " +editTaskParaVO.getDmstep2()+
             " -numdms " +editTaskParaVO.getNumdms2()+
             " -downsamp " +editTaskParaVO.getDmstep2()+
             " -mask test1_rfifind.mask -o test4 $fname");
     taskParameterMapper.addComputeParameter(taskParameterPO);
     taskParameterPO.settaskId(maxId+1);
     taskParameterPO.setstep(2);
     taskParameterPO.setstepId(3);
     taskParameterPO.setparameter("prepsubband  -numout $SPECTRA_PER_FILE/$DOWNSAMP -nsub $BYTES_PER_SPECTRA " +
             "-lodm " +editTaskParaVO.getLodm3()+
             " -dmstep " +editTaskParaVO.getDmstep3()+
             " -numdms " +editTaskParaVO.getNumdms3()+
             " -downsamp " +editTaskParaVO.getDmstep3()+
             " -mask test1_rfifind.mask -o test4 $fname");
     taskParameterMapper.addComputeParameter(taskParameterPO);
     taskParameterPO.settaskId(maxId+1);
     taskParameterPO.setstep(2);
     taskParameterPO.setstepId(4);
     taskParameterPO.setparameter("prepsubband  -numout $SPECTRA_PER_FILE/$DOWNSAMP -nsub $BYTES_PER_SPECTRA " +
             "-lodm " +editTaskParaVO.getLodm1()+
             " -dmstep " +editTaskParaVO.getDmstep4()+
             " -numdms " +editTaskParaVO.getNumdms4()+
             " -downsamp " +editTaskParaVO.getDmstep4()+
             " -mask test1_rfifind.mask -o test4 $fname");
     taskParameterMapper.addComputeParameter(taskParameterPO);
     taskParameterPO.settaskId(maxId+1);
     taskParameterPO.setstep(2);
     taskParameterPO.setstepId(5);
     taskParameterPO.setparameter("prepsubband  -numout $SPECTRA_PER_FILE/$DOWNSAMP -nsub $BYTES_PER_SPECTRA " +
             "-lodm " +editTaskParaVO.getLodm5()+
             " -dmstep " +editTaskParaVO.getDmstep5()+
             " -numdms " +editTaskParaVO.getNumdms5()+
             " -downsamp " +editTaskParaVO.getDmstep5()+
             " -mask test1_rfifind.mask -o test4 $fname");
     taskParameterMapper.addComputeParameter(taskParameterPO);
     taskParameterPO.settaskId(maxId+1);
     taskParameterPO.setstep(2);
     taskParameterPO.setstepId(6);
     taskParameterPO.setparameter("prepsubband  -numout $SPECTRA_PER_FILE/$DOWNSAMP -nsub $BYTES_PER_SPECTRA " +
             "-lodm " +editTaskParaVO.getLodm6()+
             " -dmstep " +editTaskParaVO.getDmstep6()+
             " -numdms " +editTaskParaVO.getNumdms6()+
             " -downsamp " +editTaskParaVO.getDmstep6()+
             " -mask test1_rfifind.mask -o test4 $fname");
     taskParameterMapper.addComputeParameter(taskParameterPO);
     taskParameterPO.settaskId(maxId+1);
     taskParameterPO.setstep(4);
     taskParameterPO.setstepId(1);
     taskParameterPO.setparameter("accelsearch -zmax  "+editTaskParaVO.getZmax()+" -numharm "+editTaskParaVO.getNumharm());
     taskParameterMapper.addComputeParameter(taskParameterPO);
     taskParameterPO.settaskId(maxId+1);
     taskParameterPO.setstep(5);
     taskParameterPO.setstepId(1);
     taskParameterPO.setparameter("prepfold -npart "+editTaskParaVO.getNpart()+" -start "+editTaskParaVO.getStart()+
             " -end "+editTaskParaVO.getEnd()+" -noxwin -mask $maskname -dm $psr_dm -nsub "+editTaskParaVO.getNsub()
             +" -nosearch  -o $fout1 -accelcand $psr_candnum -accelfile $psr_candfile $fname"+editTaskParaVO.getTime());
     taskParameterMapper.addComputeParameter(taskParameterPO);

     //taskParameterPO.settaskId();
     // taskParameterPO.
     //taskParameterPO*/
        return 1;
    }

    @Override
    public void modifyTaskParaByTaskId() {

    }

    public List<UserParaTree> findAllTaskIdByUserName(String userName) {

        List<TaskIdPO> lists = taskIdMapper.queryAllTaskIdByUser(userName);

        List<UserParaTree> userParaTrees = new LinkedList<UserParaTree>();
        for (int i = 0; i < lists.size(); i++) {
            UserParaTree temp = new UserParaTree();
            temp.setId(lists.get(i).getTaskId());
            temp.setText(lists.get(i).getTaskId().toString());
            temp.setChecked(false);
            userParaTrees.add(temp);
            temp = null;
        }
        return userParaTrees;
//for(int i=0;i<allTaskIdlist.size();i++){
//    taskParameterMapper.queryComputeParameterByTaskId(allTaskIdlist.get(i));
        //allTaskIdlist.

    }


    public void editParaByTaskId() {


    }

    public void addrfi() {
    }

    @Override
    public List<TaskParameterPO> findTaskParaByTaskId(TaskIdQueryVO taskIdQueryVO) {
        return taskParameterMapper.queryComputeParameterByTaskId(taskIdQueryVO);

        //  PytaskParameterMapper.queryComputeParameterByTaskId();
    }
}
